package org.lanqiao.wxq.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.lanqiao.tools.BaseDao;
import org.lanqiao.tools.BaseServlet;
import org.lanqiao.wxq.dao.PositionDao;
import org.lanqiao.wxq.dao.impl.PositionDaoImpl;

import com.google.gson.Gson;

@WebServlet("/PositionServlet")
public class PositionServlet extends BaseServlet {

	PositionDao dao = new PositionDaoImpl();
	Gson gson = new Gson();

	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.request = request;
		this.response = response;

//			代码重复了、只是sql语句不同、selectPositionOrCompany
//			当value=1是Position，2是Company
//if (values==1) {
//	sql=positionSql;
//}else if (values==2) {
//	sql=companySql;
//}			
		try {

//搜索职位、并分页			
			if ("selectPositionName".equals(getString("type"))) {
				String key = "%" + (getString("key") == null ? "" : getString("key")) + "%";
				System.out.println("selectPosition" + key);
				Integer pageIndex = isNull("pageIndex") ? 1 : getInt("pageIndex");
				Integer pageSize = isNull("pageSize") ? 15 : getInt("pageSize");
				pageIndex = (pageIndex - 1) * pageSize;
//					System.out.println(pageIndex);
				String sql = "select distinct p.*, c.*, f.name from position p, company c, founder f where p.companyId=c.id and f.companyId=c.id and p.positionName like ? limit ?,?";
				List<Map<String, Object>> query = BaseDao.query.query(sql, new MapListHandler(), key, pageIndex,pageSize);
				response.getWriter().write(gson.toJson(query));
//获取职位总行数					 
			} else if ("getCount".equals(getString("type"))) {
				String key = "%" + (getString("key") == null ? "" : getString("key")) + "%";
				String sql = "select count(position.id) as count from `position` where positionName like ?";
				Map<String, Object> query = BaseDao.query.query(sql, new MapHandler(), key);
				response.getWriter().write(gson.toJson(query));
//职位详情页					
			} else if ("positionDetail".equals(getString("type"))) {
				String key = "%" + (getString("key") == null ? "" : getString("key")) + "%";
				String sql = "SELECT p.*,c.id,c.shortName,c.address,c.com,c.number,c.field,c.logId,c.period,c.sateIds from position p,company c where p.companyId=c.id and p.positionName LIKE ?";
				List<Map<String, Object>> query = BaseDao.query.query(sql, new MapListHandler(), key);
				response.getWriter().write(gson.toJson(query));

//搜索公司、前端没有搜索公司的结果模板		 
			} else if ("selectCompanyName".equals(getString("type"))) {
				String key = "%" + (getString("key") == null ? "" : getString("key")) + "%";
				System.out.println("selectCompany" + key);
				String sql = "select distinct p.*, c.*, f.name from position p, company c, founder f where p.companyId=c.id and f.companyId=c.id and (fullName like ? or shortName like ?)";
				List<Map<String, Object>> query = BaseDao.query.query(sql, new MapListHandler(), key, key);
				response.getWriter().write(gson.toJson(query));
//热门职位、没有根据点击量查询				
			} else if ("selectHotPosition".equals(getString("type"))) {
				System.out.println("selectHotPosition");
				String sql = "select p.id as 'pid',p.positionName,p.salaryMin,p.salaryMax,p.city,p.experience,p.education,p.attraction, c.id as 'cid',c.fullName,c.number,c.field,c.labelId,c.period, f.name from position p, company c, founder f where p.companyId=c.id and f.companyId=c.id and (p.id between 1 and 8)";
				List<Map<String, Object>> query = BaseDao.query.query(sql, new MapListHandler());
				response.getWriter().write(gson.toJson(query));
//最新职位、据发布时间降序					
			} else if ("selectNewPosition".equals(getString("type"))) {
				System.out.println("selectNewPosition");
				String sql = "select p.*, c.*, f.name from position p, company c, founder f where p.companyId=c.id and f.companyId=c.id and (p.id between 1 and 8) order by p.date desc ";
				List<Map<String, Object>> query = BaseDao.query.query(sql, new MapListHandler());
				response.getWriter().write(gson.toJson(query));
			}
//查询所有标签、为了在主页显示				
			else if ("getLabelAll".equals(getString("type"))) {
				System.out.println("getLabelAll");
				String sql = "select * from label";
				List<Map<String, Object>> query = BaseDao.query.query(sql, new MapListHandler());
				response.getWriter().write(gson.toJson(query));
//创建session会话				
			} else if ("setPid".equals(getString("type"))) {
				System.out.println("setPid");
				Integer pid = getInt("pid");
				HttpSession session = request.getSession();
				session.setAttribute("pid", pid);
				String sql = "SELECT p.*,c.id,c.shortName,c.com,c.number,c.field,c.logId,c.period,c.sateIds from position p,company c where p.companyId=c.id and p.id= ?";
				List<Map<String, Object>> query = BaseDao.query.query(sql, new MapListHandler(), pid);
				response.getWriter().write(gson.toJson(query));
			} else if ("getPid".equals(getString("type"))) {
				Object pid = request.getSession().getAttribute("pid");
				System.out.println(request.getSession().getId() + "/pid=" + pid);
				String sql = "SELECT p.*,c.id,c.shortName,c.com,c.number,c.field,c.logId,c.period,c.sateIds from position p,company c where p.companyId=c.id and p.id= ?";
				List<Map<String, Object>> query = BaseDao.query.query(sql, new MapListHandler(), pid);
				response.getWriter().write(gson.toJson(query));

			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
